package com.zhiyun.utils;

import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.parser.ParseException;
import com.zhiyun.utils.StringUtil;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.CollectionUtils;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class JavaClassUtil{


    /**
     * 获取系统中全部的classes
     * @param project
     * @return
     * @throws IOException
     * @throws MojoExecutionException
     */
    public static List<JavaClass> getAllClasses(MavenProject project) {
        String name = project.getName();
        List<String> modules = project.getModules();
        List<File> javaFiles = new ArrayList<>();
        MavenProject parentProject = project;
        if(modules == null || modules.size() == 0){
            System.out.println("当前是子工程 :" + name);
            parentProject = project.getParent();
        }else {
            System.out.println("当前是父工程 :" + name);
        }

        File basedir = parentProject.getBasedir();
        if (basedir.isDirectory()) {
            try {
                javaFiles.addAll(FileUtils.getFiles(basedir, "**\\/*.java", null, true));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();
        javaProjectBuilder.setEncoding("utf-8");
        javaProjectBuilder.setErrorHandler(e -> System.err.println(e.getMessage()));
        javaProjectBuilder.addClassLoader(ClassLoaderUtil.getRuntimeClassLoader(project));

        for(File file : javaFiles){
            javaProjectBuilder.addSourceTree(file);
        }

        return (List<JavaClass>) javaProjectBuilder.getClasses();
    }

    protected static List<String> getProjectSourceRoots(MavenProject p) {
        return (p.getCompileSourceRoots() == null
                ? Collections.<String>emptyList()
                : new LinkedList<String>(p.getCompileSourceRoots()));
    }

    /**
     * 通过name获取类标签的value
     *
     * @param cls           类
     * @param tagName       需要获取的标签name
     * @param checkComments 检查注释
     * @return 类标签的value
     * @author songhaozhi
     */
    public static String getClassTagsValue(final JavaClass cls, final String tagName, boolean checkComments) {
        if (StringUtil.isNotEmpty(tagName)) {
            StringBuilder result = new StringBuilder();
            List<DocletTag> tags = cls.getTags();
            for (int i = 0; i < tags.size(); i++) {
                if (!tagName.equals(tags.get(i).getName())) {
                    continue;
                }
                String value = tags.get(i).getValue();
                if (StringUtil.isEmpty(value) && checkComments) {
                    throw new RuntimeException("ERROR: #" + cls.getName()
                            + "() - bad @" + tagName + " javadoc from " + cls.getName() + ", must be add comment if you use it.");
                }
                if (tagName.equals(tags.get(i).getName())) {
                    if (i != 0) {
                        result.append(",");
                    }
                    result.append(value);
                }
            }
            return result.toString();
        }
        return null;
    }
}
